﻿/*$().ready(function () {
    $("#breadcrumb").append('<li><a href="' + $("#urlOrganizationalUnit").val() + '" title="Departamentos">Departamentos</a></li>');
    $("#optOrganizationalUnit").addClass("HoverOnList");

    $("input[type='radio']").change(function () {
        if ($('#yes').is(':checked')) {
            $("#pnlSearchDepartment").show();
        }
        else {
            $("#pnlSearchDepartment").hide();
        }
    });
});*/

$().ready(function () {
    $("#breadcrumb").append('<li><a href="' + $("#urlOrganizationalUnit").val() + '" title="Departamentos">Departamentos</a></li>');
    $("#optOrganizationalUnit").addClass("HoverOnList");

    /*
    $("input[type='radio']").change(function () {
        if ($('#yes').is(':checked')) {
            $("#pnlSearchDepartment").show();
        }
        else {
            $("#pnlSearchDepartment").hide();
        }
    });*/

    $("#optOffice").attr("class", "ListSelectedOption");
    $("#breadcrumb").append('<li><a href="' + $("#urlProductLocation").val() + '" title="Localizacion de Producto">Localizacion de Producto</a></li>');

    ///Toolbar
    $('#btnClear').live('click', function () {
        ClearForm();
    });

    $('#btnNew').live('click', function () {
        if (confirm('¿Desea abandonar la edición y crear un registro nuevo?')) {
            window.location = $('#urlCreate').val();
        }
    });

    ///Menu
    $('#menuClear').live('click', function () {
        ClearForm();
    });

    $('#menuNew').live('click', function () {
        if (confirm('¿Desea abandonar la edición y crear un registro nuevo?')) {
            window.location = $('#urlCreate').val();
        }
    });
    $('#btnSearch').click(function () {
        Search();
    });

    $("#txtStartDate").datepicker({
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

    $("#txtEndDate").datepicker({
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

    if (!(typeof $('#txtFilter')[0] === "undefined")) {
        InitAutocomplete();
    }
});

function ConfirmDelete(id) {
    if (confirm('¿Esta seguro que desea eliminar el producto?')) {
        Delete(id);
    }
}

function Delete(objectId) {
    $.ajax(
    {
        type: "POST",
        url: $('#urlDelete').val(),
        data:
            JSON.stringify({
                id: objectId
            }),

        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                window.location = "/ODSYSUI/OrganizationalUnit/SetMessage?id=1";
            }
            else {
                $("#lblErrorText").text(result.msg);
                $("#lblError").show(300).delay(5000).hide(400);
            }
        },
        error: function (req, status, error) {
            window.location = "/ODSYSUI/OrganizationalUnit/SetMessage?id=2";
        }
    });
}

function ClearForm() {
    //Limpia los textbox
    $('#txtID').val('');
    $('#txtName').val('');
    $('#txtDescription').val('');

    //Remueve todos los mensajes de error que genera el framework
    $('.field-validation-error').remove();
    $(":input").removeClass("input-validation-error");
}

function InitAutocomplete() {
    $("#txtFilter").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                url: $('#urlGetParties').val(),
                dataType: "json",
                data: {
                    searchString: request.term,
                    IsByName: $('#rdbNameFilter').is(':checked'),
                    maxRows: 12
                },
                parse: function (data) {
                    var rows = new Array();
                    for (var i = 0; i < data.length; i++) {
                        rows[i] = { data: data[i], value: data[i].name, result: data[i].name };
                    }
                    return rows;
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.identification + " - " + item.name,
                            ID: item.partyID,
                            PartyID: item.partyID,
                            Code: item.identification,
                            Name: item.name
                        }
                    }));
                },
                error: function (data, status, e) {
                    alert(e);
                }
            });
        },
        minLength: 0,
        select: function (event, ui) {
            $('#fromRoleID').val(ui.item.ID);
            $('#partyCode').val(ui.item.identification);
            $('#partyName').val(ui.item.Name);
        }
    });

    $("#txtFilter").keyup(function () {
        if (!this.value) {
            $('#fromRoleID').val();
        }
    });
}

function Search() {
    var startDate = $('#txtStartDate').val();
    var endDate = $('#txtEndDate').val();
    var date1 = null;
    var date2 = null;

    if (startDate != "") {
        var d1 = parseInt(startDate.substring(0, 2));
        var m1 = parseInt(startDate.substring(3, 5)) - 1;
        var y1 = parseInt(startDate.substring(6, 10));
        date1 = new Date(y1, m1, d1);
    }

    if (endDate != "") {
        var d1 = parseInt(endDate.substring(0, 2));
        var m1 = parseInt(endDate.substring(3, 5)) - 1;
        var y1 = parseInt(endDate.substring(6, 10));
        date2 = new Date(y1, m1, d1);
    }

    $.ajax(
    {
        type: "POST",
        traditional: true,
        url: $("#urlSearch").val(),
        secureuri: false,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        enctype: 'multipart/form-data',
        data:
            JSON.stringify({
                name: $('#txtName').val(),
                startDate: date1,
                endDate: date2,
            }),

        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                window.location = $("#urlSearch").val();
            }
            else {
                $("#lblErrorText").text(result.msg);
                $("#lblError").show(300).delay(5000).hide(400);
            }
        },
        error: function (req, status, error) {
            debugger;
        }
    });
}